package com.paw.ruoyi.admin.core.util;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.paw.ruoyi.admin.common.constant.CommonConstant;

/**
 * request Params 参数签名
 * @author lvzihai
 * @date 2022/3/25
 **/
public class SignUtils {


  public static final String[] SKIP_PARAMS = {CommonConstant.SIGN_PARAM, CommonConstant.TIME_PARAM};


  /**
   * 获取param参数签名
   * 参数名称排序key，值为数组时逗号拼接成字符串
   * secret+ key+value + timeStamp
   * @param secret
   * @param timeStamp
   * @return
   */
  public static String getSign(Map<String, String[]> parameterMap, String secret,String timeStamp){
    List<String> paramNames = parameterMap.keySet().stream()
        .sorted()
        .collect(Collectors.toList());
    StringBuilder builder = new StringBuilder();
    builder.append(secret);

    for (String paramName : paramNames) {
      if(!ArrayUtil.contains(SKIP_PARAMS,paramName)){
        String[] values = parameterMap.get(paramName);
        String parameterValue = values.length==1? values[0]:ArrayUtil.join(values,",");
        builder.append(paramName);
        builder.append(parameterValue);
      }
    }
    builder.append(timeStamp);

    return DigestUtil.md5Hex(builder.toString());
  }

//  public static void main (String[] args) {
//
//  }

}
